/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.sciPersonnelInfo.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.sciPersonnelInfo.domain.*;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.ModelMateDemandsDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoDto;
import com.fzhucc.modules.sciPersonnelInfo.repository.SciPersonnelInfoRepository;
import com.fzhucc.modules.sciPersonnelInfo.service.mapstruct.SciPersonnelInfoMapper;
import com.fzhucc.modules.sciTechnologyDemand.domain.SciTechnologyDemand;
import com.fzhucc.modules.sciTechnologyDemand.domain.SciTechnologyDemandInfo;
import com.fzhucc.modules.sciTechnologyDemand.domain.SciTechnologyDemandMate;
import com.fzhucc.modules.sciTechnologyDemand.domain.dto.*;
import com.fzhucc.modules.sciTechnologyDemand.repository.SciTechnologyDemandInfoRepository;
import com.fzhucc.modules.sciTechnologyDemand.repository.SciTechnologyDemandMateRepository;
import com.fzhucc.modules.sciTechnologyDemand.repository.SciTechnologyDemandRepository;
import com.fzhucc.modules.sciTechnologyDemand.service.mapstruct.SciTechnologyDemandInfoMapper;
import com.fzhucc.modules.sciTechnologyDemand.service.mapstruct.SciTechnologyDemandMapper;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.sciPersonnelInfo.repository.SciPersonnelInfoMateRepository;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoMateDto;
import com.fzhucc.modules.sciPersonnelInfo.domain.dto.SciPersonnelInfoMateQueryCriteria;
import com.fzhucc.modules.sciPersonnelInfo.service.mapstruct.SciPersonnelInfoMateMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-01-08
 **/
@Service
@RequiredArgsConstructor
public class SciPersonnelInfoMateService {

    private final SciPersonnelInfoMateRepository sciPersonnelInfoMateRepository;

    private final SciTechnologyDemandMateRepository sciTechnologyDemandMateRepository;

    private final SciTechnologyDemandInfoRepository sciTechnologyDemandInfoRepository;
    private final SciPersonnelInfoMateMapper sciPersonnelInfoMateMapper;

    private final SciTechnologyDemandRepository tdRepository;

    private final SciTechnologyDemandMapper tdMapper;

    private final SciTechnologyDemandInfoMapper sciTechnologyDemandInfoMapper;

    private final SciPersonnelInfoRepository spRepository;

    private final SciPersonnelInfoMapper spMapper;

    @Value("${sci.audit.shtg}")
    public String shtg;

    public Map<String, Object> queryAll(SciPersonnelInfoMateQueryCriteria criteria, Pageable pageable) {
        if (criteria.getName() != null) {
            Page<SciPersonnelInfoMate> page2 = sciPersonnelInfoMateRepository.findAllWithOutParmByLikeMName(criteria.getSpId(), "%" + criteria.getName() + "%", pageable);
            Page<SciPersonnelInfoMateDto> map = page2.map(sciPersonnelInfoMateMapper::toDto);
            List<SciPersonnelInfoMateDto> list = map.getContent();
            if (list != null && list.size() > 0) {
                for (SciPersonnelInfoMateDto dto : list) {
                    Integer spId = dto.getSpId();
                    Optional<SciPersonnelInfo> res = spRepository.findById(spId);
                    if (res.isPresent()) {
                        SciPersonnelInfo sciPersonnelInfo = res.get();
                        dto.setSciPersonnelInfoDto(spMapper.toDto(sciPersonnelInfo));
                    }

                    Integer tdId = dto.getTdId();
                    Optional<SciTechnologyDemand> res2 = tdRepository.findById(tdId);
                    if (res2.isPresent()) {
                        SciTechnologyDemand sciTechnologyDemand = res2.get();
                        dto.setSciTechnologyDemandDto(tdMapper.toDto(sciTechnologyDemand));
                    }
                }
            }
            return PageUtil.toPage(map);
        } else {
            Page<SciPersonnelInfoMate> page = sciPersonnelInfoMateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
            Page<SciPersonnelInfoMateDto> map = page.map(sciPersonnelInfoMateMapper::toDto);
            List<SciPersonnelInfoMateDto> list = map.getContent();
            if (list != null && list.size() > 0) {
                for (SciPersonnelInfoMateDto dto : list) {
                    Integer spId = dto.getSpId();
                    Optional<SciPersonnelInfo> res = spRepository.findById(spId);
                    if (res.isPresent()) {
                        SciPersonnelInfo sciPersonnelInfo = res.get();
                        dto.setSciPersonnelInfoDto(spMapper.toDto(sciPersonnelInfo));
                    }

                    Integer tdId = dto.getTdId();
                    Optional<SciTechnologyDemand> res2 = tdRepository.findById(tdId);
                    if (res2.isPresent()) {
                        SciTechnologyDemand sciTechnologyDemand = res2.get();
                        dto.setSciTechnologyDemandDto(tdMapper.toDto(sciTechnologyDemand));
                    }
                }
            }
            return PageUtil.toPage(map);
        }
    }

    public List<SciPersonnelInfoMateDto> queryAll(SciPersonnelInfoMateQueryCriteria criteria) {
        return sciPersonnelInfoMateMapper.toDto(sciPersonnelInfoMateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Transactional
    public SciPersonnelInfoMateDto findById(Integer id) {
        SciPersonnelInfoMate sciPersonnelInfoMate = sciPersonnelInfoMateRepository.findById(id).orElseGet(SciPersonnelInfoMate::new);
        ValidationUtil.isNull(sciPersonnelInfoMate.getId(), "SciPersonnelInfoMate", "id", id);
        return sciPersonnelInfoMateMapper.toDto(sciPersonnelInfoMate);
    }

    @Transactional(rollbackFor = Exception.class)
    public SciPersonnelInfoMateDto create(SciPersonnelInfoMate resources) {
        Integer tdId = resources.getTdId();
        Optional<SciTechnologyDemand> res = tdRepository.findById(tdId);
        if (!res.isPresent()) {
            throw new BadRequestException("未找到对应的企业需求");
        }
        Integer spId = resources.getSpId();
        Optional<SciPersonnelInfo> res2 = spRepository.findById(spId);
        if (!res2.isPresent()) {
            throw new BadRequestException("未找到对应的劳模工匠");
        }
        //查询库里是否有 spid 及 tdid相同的记录 有则跳过
        List<SciPersonnelInfoMate> lists = sciPersonnelInfoMateRepository.findBySpIdTdId(spId, tdId);
        if (lists != null && lists.size() > 0) {
            throw new BadRequestException("已存在该企业需求,请选择其他企业需求");
        } else {
            SciTechnologyDemand sciTechnologyDemand = res.get();
            SciPersonnelInfo sciPersonnelInfo = res2.get();
            String sptechnology = sciPersonnelInfo.getTechnology();
            String tdtechnology = sciTechnologyDemand.getTechnology();
            String spretype = sciPersonnelInfo.getRetype();
            String tdretype = sciTechnologyDemand.getRetype();
            if (sptechnology != null && tdtechnology != null && sptechnology.equals(tdtechnology)) {
                resources.setTechnology(tdtechnology);
            }
            if (spretype != null && tdretype != null && spretype.equals(tdretype)) {
                resources.setRetype(spretype);
            }
            if (sptechnology != null && tdtechnology != null && (!sptechnology.equals(tdtechnology))) {
                resources.setTechnology("劳模工匠:" + sptechnology + ";企业需求:" + tdtechnology);
            }
            if (spretype != null && tdretype != null && (!spretype.equals(tdretype))) {
                resources.setRetype("劳模工匠:" + spretype + ";企业需求:" + tdretype);
            }
            resources.setMatesort(0);//手动添加表示为最重要的 优选顺序第一
            resources.setMateTime(new Timestamp(new Date().getTime()));
            resources.setIsSelect(0);
            return sciPersonnelInfoMateMapper.toDto(sciPersonnelInfoMateRepository.save(resources));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(SciPersonnelInfoMate resources) {
        SciPersonnelInfoMate sciPersonnelInfoMate = sciPersonnelInfoMateRepository.findById(resources.getId()).orElseGet(SciPersonnelInfoMate::new);
        ValidationUtil.isNull(sciPersonnelInfoMate.getId(), "SciPersonnelInfoMate", "id", resources.getId());
        sciPersonnelInfoMate.copy(resources);
        sciPersonnelInfoMateRepository.save(sciPersonnelInfoMate);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            sciPersonnelInfoMateRepository.deleteById(id);
        }
    }

    public void download(List<SciPersonnelInfoMateDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SciPersonnelInfoMateDto sciPersonnelInfoMate : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("劳模id", sciPersonnelInfoMate.getSpId());
            map.put("匹配时间", sciPersonnelInfoMate.getMateTime());
            map.put("匹配的技术领域", sciPersonnelInfoMate.getTechnology());
            map.put("匹配的需求类别", sciPersonnelInfoMate.getRetype());
            map.put("匹配上的企业需求id", sciPersonnelInfoMate.getTdId());
            map.put("匹配顺序后台匹配排序    按0123的顺序  0表示手动添加的顺序", sciPersonnelInfoMate.getMatesort());
            map.put("是否选中", sciPersonnelInfoMate.getIsSelect());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public List<SciTechnologyDemandDto> getAllMateSciDemandsInfo(String name) {
        if (name == null) {
            //就查询前50条数据
            List<SciTechnologyDemand> normal = tdRepository.findNormalTop(shtg);
            List<SciTechnologyDemandDto> sciTechnologyDemandDtos = tdMapper.toDto(normal);

            if (sciTechnologyDemandDtos != null && sciTechnologyDemandDtos.size() > 0) {
                for (SciTechnologyDemandDto sciTechnologyDemandDto : sciTechnologyDemandDtos) {
                    String matype = sciTechnologyDemandDto.getMaType();//技术需求的
                    if (matype != null && matype.contains(";")) {
                        String[] array = matype.split(";");
                        sciTechnologyDemandDto.setMatypes(array);
                    } else {
                        String[] array = {};
                        sciTechnologyDemandDto.setMatypes(array);
                    }
                    String orgArea = sciTechnologyDemandDto.getOrgArea();
                    if (orgArea != null && orgArea.contains(";")) {
                        String[] array = orgArea.split(";");
                        sciTechnologyDemandDto.setOrgAreas(array);
                    } else {
                        String[] array = {};
                        sciTechnologyDemandDto.setOrgAreas(array);
                    }

                    Integer id = sciTechnologyDemandDto.getId();
                    List<SciTechnologyDemandInfo> results = sciTechnologyDemandInfoRepository.findByTdId(id);
                    sciTechnologyDemandDto.setListSdInfo(sciTechnologyDemandInfoMapper.toDto(results));

                }
            }
            return sciTechnologyDemandDtos;
        } else {
            List<SciTechnologyDemand> list = tdRepository.findNormalByLikeName(shtg, "%" + name + "%");
            List<SciTechnologyDemandDto> sciTechnologyDemandDtos = tdMapper.toDto(list);

            if (sciTechnologyDemandDtos != null && sciTechnologyDemandDtos.size() > 0) {
                for (SciTechnologyDemandDto sciTechnologyDemandDto : sciTechnologyDemandDtos) {
                    String matype = sciTechnologyDemandDto.getMaType();
                    if (matype != null && matype.contains(";")) {
                        String[] array = matype.split(";");
                        sciTechnologyDemandDto.setMatypes(array);
                    } else {
                        String[] array = {};
                        sciTechnologyDemandDto.setMatypes(array);
                    }
                    String orgArea = sciTechnologyDemandDto.getOrgArea();
                    if (orgArea != null && orgArea.contains(";")) {
                        String[] array = orgArea.split(";");
                        sciTechnologyDemandDto.setOrgAreas(array);
                    } else {
                        String[] array = {};
                        sciTechnologyDemandDto.setOrgAreas(array);
                    }
                    Integer id = sciTechnologyDemandDto.getId();
                    List<SciTechnologyDemandInfo> results = sciTechnologyDemandInfoRepository.findByTdId(id);
                    sciTechnologyDemandDto.setListSdInfo(sciTechnologyDemandInfoMapper.toDto(results));
                }
            }
            return sciTechnologyDemandDtos;
        }
    }


    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> mateWithMateSort(SciPersonnelInfoMateQueryCriteria criteria, Pageable pageable) {
        //TODO
        // 匹配的优先级
        // 1所属工会+技术领域+需求类别；
        // 2技术领域+需求类别；
        // 3技术领域
        //List<SciTechnologyDemandDto> result = new ArrayList<SciTechnologyDemandDto>();
        Integer spmodelId = criteria.getSpId();
        Optional<SciPersonnelInfo> res = spRepository.findById(spmodelId);
        if (!res.isPresent()) {
            throw new BadRequestException("未找到对应的劳模工匠");
        }
        SciPersonnelInfo sciPersonnelInfo1 = res.get();
        String technology = sciPersonnelInfo1.getTechnology();//技术领域
        String retype = sciPersonnelInfo1.getRetype();//需求类别    多个
        String orgArea = sciPersonnelInfo1.getOrgArea();//需求的orgArea 例福建省;福州市;马尾区

        String[] arrretype = {};
        if (retype != null && !retype.equals("")) {
            if (retype.contains(";")) {
                arrretype = retype.split(";");
            }
        }
        String sorgArea = "%" + orgArea + "%";

        //todo 先查询被选中的list 此 list不再新增
        List<Integer> listTdIds = new ArrayList<Integer>();
        List<SciPersonnelInfoMate> isSelect = sciPersonnelInfoMateRepository.findIsSelect(spmodelId);
        if (isSelect != null && isSelect.size() > 0) {
            for (SciPersonnelInfoMate mate : isSelect) {
                listTdIds.add(mate.getTdId());
            }
        }

        // 先按顺序查询记录  最后统一查询
        if (arrretype.length > 0) {
            for (String sretype : arrretype) {
                List<SciTechnologyDemand> list = tdRepository.findNormalLikeTechnologyRetypeLikeRetypeLikeOrgArea(technology, sretype, sorgArea, shtg);
                if (list != null && list.size() > 0) {
                    for (SciTechnologyDemand sciTechnologyDemand : list) {
                        List<SciPersonnelInfoMate> lists = sciPersonnelInfoMateRepository.findBySpIdTdId(spmodelId, sciTechnologyDemand.getId());
                        if (lists != null && lists.size() > 0) {
                            //存在不操作
                        } else {
                            if (!listTdIds.contains(sciTechnologyDemand.getId())) {
                                SciPersonnelInfoMate sciPersonnelInfoMate = new SciPersonnelInfoMate();
                                sciPersonnelInfoMate.setTechnology(sciTechnologyDemand.getTechnology());
                                sciPersonnelInfoMate.setRetype(sciTechnologyDemand.getMaType());
                                sciPersonnelInfoMate.setMateTime(new Timestamp(new Date().getTime()));
                                sciPersonnelInfoMate.setSpId(spmodelId);
                                sciPersonnelInfoMate.setTdId(sciTechnologyDemand.getId());
                                sciPersonnelInfoMate.setMatesort(1);
                                sciPersonnelInfoMate.setIsSelect(0);
                                sciPersonnelInfoMateRepository.save(sciPersonnelInfoMate);
                            }
                        }
                    }
                }
            }
        }

        // 先按顺序查询记录  最后统一查询
        if (arrretype.length > 0) {
            for (String sretype : arrretype) {
                List<SciTechnologyDemand> list2 = tdRepository.findNormalByTechnologyRetype(technology, sretype, shtg);
                if (list2 != null && list2.size() > 0) {
                    for (SciTechnologyDemand sciTechnologyDemand : list2) {
                        List<SciPersonnelInfoMate> lists = sciPersonnelInfoMateRepository.findBySpIdTdId(spmodelId, sciTechnologyDemand.getId());
                        if (lists != null && lists.size() > 0) {
                            //存在不操作
                        } else {
                            if (!listTdIds.contains(sciTechnologyDemand.getId())) {
                                SciPersonnelInfoMate sciPersonnelInfoMate = new SciPersonnelInfoMate();
                                sciPersonnelInfoMate.setTechnology(sciTechnologyDemand.getTechnology());
                                sciPersonnelInfoMate.setRetype(sciTechnologyDemand.getMaType());
                                sciPersonnelInfoMate.setMateTime(new Timestamp(new Date().getTime()));
                                sciPersonnelInfoMate.setSpId(spmodelId);
                                sciPersonnelInfoMate.setTdId(sciTechnologyDemand.getId());
                                sciPersonnelInfoMate.setMatesort(2);
                                sciPersonnelInfoMate.setIsSelect(0);
                                sciPersonnelInfoMateRepository.save(sciPersonnelInfoMate);
                            }
                        }
                    }
                }
            }
        }

        List<SciTechnologyDemand> list3 = tdRepository.findNormalByLikeTechnology(technology, shtg);
        if (list3 != null && list3.size() > 0) {
            for (SciTechnologyDemand sciTechnologyDemand : list3) {
                List<SciPersonnelInfoMate> lists = sciPersonnelInfoMateRepository.findBySpIdTdId(spmodelId, sciTechnologyDemand.getId());
                if (lists != null && lists.size() > 0) {
                    //存在不操作
                } else {
                    if (!listTdIds.contains(sciTechnologyDemand.getId())) {
                        SciPersonnelInfoMate sciPersonnelInfoMate = new SciPersonnelInfoMate();
                        sciPersonnelInfoMate.setTechnology(sciTechnologyDemand.getTechnology());
                        sciPersonnelInfoMate.setRetype(sciTechnologyDemand.getMaType());
                        sciPersonnelInfoMate.setMateTime(new Timestamp(new Date().getTime()));
                        sciPersonnelInfoMate.setSpId(spmodelId);
                        sciPersonnelInfoMate.setTdId(sciTechnologyDemand.getId());
                        sciPersonnelInfoMate.setMatesort(3);
                        sciPersonnelInfoMate.setIsSelect(0);
                        sciPersonnelInfoMateRepository.save(sciPersonnelInfoMate);
                    }
                }
            }
        }
        //TODO查数据库此需求是否有匹配过有则返回list
        /*List<SciPersonnelInfoMate> listModelsMates = sciPersonnelInfoMateRepository.findBySpId(sciPersonnelInfo1.getId());
        if (listModelsMates != null && listModelsMates.size() > 0) {
            for (SciPersonnelInfoMate sciPersonnelInfoMate : listModelsMates) {
                Integer tdId = sciPersonnelInfoMate.getTdId();
                Optional<SciTechnologyDemand> ops = tdRepository.findById(tdId);
                if (ops.isPresent()) {
                    SciTechnologyDemand sciTechnologyDemand = ops.get();
                    result.add(tdMapper.toDto(sciTechnologyDemand));
                }
            }
            return result;
        } else {
            throw new BadRequestException("未匹配到符合的对应类型");
        }*/

        Page<SciPersonnelInfoMate> pages = sciPersonnelInfoMateRepository.findBySpIdPage(criteria.getSpId(), pageable);
        Page<SciPersonnelInfoMateDto> map = pages.map(sciPersonnelInfoMateMapper::toDto);
        List<SciPersonnelInfoMateDto> lists = map.getContent();
        if (lists != null && lists.size() > 0) {
            for (SciPersonnelInfoMateDto sciPersonnelInfoMateDto : lists) {
                Integer tdId = sciPersonnelInfoMateDto.getTdId();
                Optional<SciTechnologyDemand> ops = tdRepository.findById(tdId);
                if (ops.isPresent()) {
                    SciTechnologyDemand sciTechnologyDemand = ops.get();
                    sciPersonnelInfoMateDto.setSciTechnologyDemandDto(tdMapper.toDto(sciTechnologyDemand));
                }
            }
        }
        return PageUtil.toPage(map);
    }

    @Transactional(rollbackFor = Exception.class)
    public void select(ListSelectModelDto2 list) {
        if (list == null) {
            throw new BadRequestException("未找到匹配集合列表");
        }
        List<SelectModelDto2> res = list.getLists();
        for (SelectModelDto2 dto : res) {
            Integer id = dto.getId();
            Optional<SciPersonnelInfoMate> ops = sciPersonnelInfoMateRepository.findById(id);
            if (ops.isPresent()) {
                SciPersonnelInfoMate sciPersonnelInfoMate = ops.get();
                sciPersonnelInfoMate.setIsSelect(1);
                SciPersonnelInfoMate save = sciPersonnelInfoMateRepository.save(sciPersonnelInfoMate);

                //todo 劳模选中需求同理
                Integer spId = save.getSpId();
                Integer tdId = save.getTdId();
                List<SciTechnologyDemandMate> listModelMate = sciTechnologyDemandMateRepository.findBySpIdTdId(spId, tdId);
                if (listModelMate != null && listModelMate.size() == 1) {
                    SciTechnologyDemandMate sciTechnologyDemandMate = listModelMate.get(0);
                    sciTechnologyDemandMate.setIsSelect(1);
                    sciTechnologyDemandMate.setMatesort(-1);//最高级顺序
                    sciTechnologyDemandMate.setMateTime(new Timestamp(new Date().getTime()));
                    sciTechnologyDemandMateRepository.save(sciTechnologyDemandMate);//设置为选中状态
                } else {
                    if (listModelMate == null || listModelMate.size() == 0) {
                        SciTechnologyDemandMate sciTechnologyDemandMate = new SciTechnologyDemandMate();
                        sciTechnologyDemandMate.setIsSelect(1);
                        sciTechnologyDemandMate.setSpId(spId);
                        sciTechnologyDemandMate.setTdId(tdId);
                        sciTechnologyDemandMate.setMatesort(-1);//最高级顺序
                        sciTechnologyDemandMate.setMateTime(new Timestamp(new Date().getTime()));
                        sciTechnologyDemandMateRepository.save(sciTechnologyDemandMate);//设置为选中状态
                    }
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void cancelSelect(ListSelectModelDto2 list) {
        if (list == null) {
            throw new BadRequestException("未找到匹配集合列表");
        }
        List<SelectModelDto2> res = list.getLists();
        for (SelectModelDto2 dto : res) {
            Integer id = dto.getId();
            Optional<SciPersonnelInfoMate> ops = sciPersonnelInfoMateRepository.findById(id);
            if (ops.isPresent()) {
                SciPersonnelInfoMate sciPersonnelInfoMate = ops.get();
                sciPersonnelInfoMate.setIsSelect(0);
                SciPersonnelInfoMate save = sciPersonnelInfoMateRepository.save(sciPersonnelInfoMate);

                Integer spId = save.getSpId();
                Integer tdId = save.getTdId();
                //取消选则
                List<SciTechnologyDemandMate> listModelMate = sciTechnologyDemandMateRepository.findBySpIdTdId(spId, tdId);
                if (listModelMate != null && listModelMate.size() == 1) {
                    SciTechnologyDemandMate sciTechnologyDemandMate = listModelMate.get(0);
                    sciTechnologyDemandMate.setIsSelect(0);//取消选择
                    //sciTechnologyDemandMate.setMateSort(-1);
                    sciTechnologyDemandMate.setMateTime(new Timestamp(new Date().getTime()));
                    sciTechnologyDemandMateRepository.save(sciTechnologyDemandMate);
                } else {
                    if (listModelMate == null || listModelMate.size() == 0) {
                        SciTechnologyDemandMate sciTechnologyDemandMate = new SciTechnologyDemandMate();
                        sciTechnologyDemandMate.setIsSelect(0);
                        sciTechnologyDemandMate.setSpId(spId);
                        sciTechnologyDemandMate.setTdId(tdId);
                        //sciTechnologyDemandMate.setMateSort(-1);
                        sciTechnologyDemandMate.setMateTime(new Timestamp(new Date().getTime()));
                        sciTechnologyDemandMateRepository.save(sciTechnologyDemandMate);
                    }
                }
            }
        }
    }


}